<?php

require_once 'BaseHelper.class.php';

class DynamicModuleHelper extends BaseHelper {

	public static $MODULE_NAME = "DynamicModule"; //数据库操作的模块名

	//------------------------------------基本函数开始----------------------------------------
	public static function add($data,$displaySQL = false) {
		return parent::add ( self::$MODULE_NAME, $data,$displaySQL );
	}
	public static function update($data, $whereStr="",$displaySQL = false) {
		return parent::update ( self::$MODULE_NAME, $data, $whereStr,$displaySQL );
	}
	public static function updateSQL($sql,$displaySQL = false) {
		return parent::updateSQL( self::$MODULE_NAME, $sql,$displaySQL);
	}
	public static function delete($whereStr="",$displaySQL = false) {
		return parent::delete ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function getObject($whereStr="",$displaySQL = false) {
		return parent::getObject ( self::$MODULE_NAME, $whereStr ,$displaySQL);
	}
	public static function queryObject($sql,$displaySQL = false) {
		return parent::queryObject ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function queryForInt($sql,$displaySQL = false) {
		return parent::queryForInt ( self::$MODULE_NAME, $sql ,$displaySQL);
	}
	public static function getList($whereStr = '', $orderStr = '', $limitNum = 0,$displaySQL = false) {
		return parent::getList ( self::$MODULE_NAME, $whereStr, $orderStr, $limitNum ,$displaySQL);
	}
	public static function queryList($sql = '', $displaySQL = false) {
		return parent::queryList(self::$MODULE_NAME,$sql,$displaySQL);
	}
	//------------------------------------基本函数结束----------------------------------------


	//------------------------------------自定义函数开始--------------------------------------
    
	/**
	 * 添加【事务控制】
	 */
	public static function addWithTrans($data) {
		
		//1....增加一个DynamicModule
		//2....dynamic_module_field表进行相应的记录
		
		$model = M();
		$model->startTrans();
		
		//1....增加一个DynamicModule
		$resultid = DynamicModuleHelper::add($data);
		
		//2....dynamic_module_field表进行相应的记录【记录系统字段 id、seo_keywords、seo_description、catalog_id、view_num、redirect_url、timeline】
		//记录id字段
		$data2["dynamic_module_id"] = $resultid;
		$data2["field_name"] = "id";
		$data2["field_desc"] = 'ID';
		$data2["form_type"] = FormType::$NUMBER;
		$data2["is_system"] = 1;
		$data2["order_num"] = 88888;
		$result2 = DynamicModuleFieldHelper::add($data2);
		
		//记录seo_keywords字段
		$data3["dynamic_module_id"] = $resultid;
		$data3["field_name"] = "seo_keywords";
		$data3["field_desc"] = 'SEO关键词';
		$data3["form_type"] = FormType::$INPUT;
		$data3["is_system"] = 1;
		$data3["order_num"] = 0;
		$result3 = DynamicModuleFieldHelper::add($data3);
		
		//记录seo_description字段
		$data4["dynamic_module_id"] = $resultid;
		$data4["field_name"] = "seo_description";
		$data4["field_desc"] = 'SEO描述';
		$data4["form_type"] = FormType::$TEXTAREA;
		$data4["is_system"] = 1;
		$data4["order_num"] = 0;
		$result4 = DynamicModuleFieldHelper::add($data4);
		
		//记录catalog_id字段
		$data5["dynamic_module_id"] = $resultid;
		$data5["field_name"] = "catalog_id";
		$data5["field_desc"] = '栏目ID';
		$data5["form_type"] = FormType::$NUMBER;
		$data5["is_system"] = 1;
		$data5["order_num"] = 0;
		$result5 = DynamicModuleFieldHelper::add($data5);
		
		//记录view_num字段
		$data6["dynamic_module_id"] = $resultid;
		$data6["field_name"] = "view_num";
		$data6["field_desc"] = '浏览量';
		$data6["form_type"] = FormType::$NUMBER;
		$data6["is_system"] = 1;
		$data6["order_num"] = 0;
		$result6 = DynamicModuleFieldHelper::add($data6);
		
		//记录redirect_url字段
		$data7["dynamic_module_id"] = $resultid;
		$data7["field_name"] = "redirect_url";
		$data7["field_desc"] = '跳转URL';
		$data7["form_type"] = FormType::$INPUT;
		$data7["is_system"] = 1;
		$data7["order_num"] = 0;
		$result7 = DynamicModuleFieldHelper::add($data7);
		
		//记录timeline字段
		$data8["dynamic_module_id"] = $resultid;
		$data8["field_name"] = "timeline";
		$data8["field_desc"] = '发布时间';
		$data8["form_type"] = FormType::$NUMBER;
		$data8["is_system"] = 1;
		$data8["order_num"] = 0;
		$result8 = DynamicModuleFieldHelper::add($data8);
		
		if($resultid !== false && $result2 !== false && $result3 !== false && $result4 !== false && $result5 !== false && $result6 !== false && $result7 !== false && $result8 !== false){
			//DB COMMIIT
			$model->commit();
			return true;
		}else{
			$model->rollback();
			return false;
		}
		
	}
	
	/**
	 * 删除【事务控制】
	 */
	public static function deleteWithTrans($id) {
		
		$model = M();
		$model->startTrans();
		
		//1....删除DynamicModule
		$result1 = DynamicModuleHelper::delete("dynamic_module_id={$id}");
		
		//2....删除DynamicModule对应的Field
		$result2 = DynamicModuleFieldHelper::delete("dynamic_module_id={$id}");		
		
		if($result1 !== false && $result2 !== false){
			//DB COMMIIT
			$model->commit();
			return true;
		}else{
			$model->rollback();
			return false;
		}
		
	}
	
	
	//------------------------------------自定义函数结束--------------------------------------


}